package com.smartEdu.controller.app;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.response.Response;
import com.smartEdu.service.NewsService;

@Controller
@RequestMapping(value="/app")
public class NewsController extends AppBaseController{
	@Autowired
	@Qualifier(value="appresponse")
	private Response respContext;
	
	@Autowired
	@Qualifier(value="newsServiceImpl")
	private NewsService newsService;
	
	@RequestMapping(value="/homepage/news",method=RequestMethod.POST)//该请求不读数据库，直接从缓存中取
	public void HomePageNotice(HttpServletRequest request,HttpServletResponse response){
		NewsTips(request,response);
	}
	
	@RequestMapping(value="/learn/news",method=RequestMethod.POST)
	public void HomePageNews(HttpServletRequest request,HttpServletResponse response){
		Session sess=null;
		try {
			sess=GetSession();
			sess.put(Tag_Tips.REQUEST, GetParameter(request));
			newsService.getNewsByNtype(sess);;
		} 
		catch (ServiceException e) {
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.ERROR);
		}
		finally {
			SetResponse(response, sess);
		}
	}
	
	public void NewsTips(HttpServletRequest request,HttpServletResponse response){
		Session sess=null;
		try {
			sess=GetSession();//获取InterceptorsApp类下的创建的session
			sess.put(Tag_Tips.REQUEST, GetParameter(request));//将post过来的请求放入session
			newsService.getNewsFromRedis(sess);//调用NewsServiceImpl的getNewsFromRedis方法
		}
		catch (ServiceException e) {//业务流程错误
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.ERROR);
		}
		finally {
			SetResponse(response, sess);//调用父类方法返回响应json
		}
	}
	
	@RequestMapping(value="homepage/news/detail",method=RequestMethod.POST)
	public void ShowNews(HttpServletRequest request,HttpServletResponse response){
		Session sess=null;
		try {
			sess=GetSession();
			sess.put(Tag_Tips.REQUEST, GetParameter(request));
			newsService.getNews(sess);
		} 
		catch (ServiceException e) {
			sess.put(Tag_Tips.RESPCODE, Tag_Tips.ERROR);
		}
		finally {
			SetResponse(response, sess);
		}
	}

	@Override
	public String GetRespContent(Session sess) {
		return respContext.GetRespContent(sess);
	}
}
